<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
const route = useRoute()
// table
const tableData = ref([])
// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const background = ref(false)

const handleSizeChange = (size: number) => {
  pageSize.value = size
  initTableData()
}

const handleCurrentChange = (page: number) => {
  currentPage.value = page
  initTableData()
}
const module = route.path.split('/').pop() || 'home'

const column = ref([])
import(`../columns/${module}.ts`).then(res => (column.value = res.default))

const initTableData = async () => {
  const { featchData } = await import(`../api/${module}.ts`)
  const res = await featchData()
  tableData.value = res.data
  total.value = res.total
}
onMounted(() => {
  initTableData()
})
</script>

<template>
  <el-table :data="tableData" border>
    <el-table-column v-for="item in column" :prop="item.key" :label="item.label" :width="item.width" :align="item.align">
      <template #default="scope">
        <el-image v-if="item.key === 'avatar'" :src="scope.row[item.key]" fit="cover" :preview-src-list="[scope.row[item.key]]" />
        <el-switch v-if="item.key === 'status'" v-model="scope.row[item.key]" :active-value="1" :inactive-value="0" />
      </template>
    </el-table-column>
  </el-table>

  <el-pagination
    v-model:current-page="currentPage"
    v-model:page-size="pageSize"
    :page-sizes="[10, 20, 30, 40]"
    :background="background"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  />
</template>
